package com.example.yibu.modelConfig;

import com.alibaba.fastjson2.JSONObject;
import com.example.yibu.modelConfig.ascPool.AsyncFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Callable;
import java.util.concurrent.CompletableFuture;
import java.util.function.Predicate;

@Component
public class TestAscPoolInfo {

    @Autowired
    private AsyncFactory asyncFactory;

    /**
     * 执行轮询任务，直到任务完成或达到最大尝试次数。
     *
     * @param orderId 订单ID
     * @param taskStatus 初始任务状态
     * @return JSONObject 包含最终结果的JSON对象
     */
    private JSONObject pollUntilComplete(String orderId, String taskStatus) throws Exception {
        if (!"PROCESSING".equals(taskStatus)) {
            throw new IllegalArgumentException("任务状态不是 PROCESSING");
        }

        Callable<JSONObject> task = () -> {
            Map<String, Object> bodyMap = new HashMap<>(); //请求参数
            Map<String, String> getHead = Map.of("Authorization", ""); //请求头
            String resValue =  ""; //HttpUtils.doGet(REMOTE_REQ_API.VIDEO_QUERY_URL + "/" + orderId, bodyMap, getHead);  //发起get请求
            return JSONObject.parseObject(resValue);
        };

        //检查以上获取得到结果是否符合要求
        Predicate<JSONObject> isComplete = resObje -> "SUCCESS".equals(resObje.getString("task_status"));

        CompletableFuture<JSONObject> future = asyncFactory.executeForResult(task, isComplete, 5000, 200);
        return future.get(); // 阻塞等待直到任务完成
    }

}
